package com.test.particles;

import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.nio.FloatBuffer;
import java.nio.ShortBuffer;

import javax.microedition.khronos.opengles.GL10;

public class Circle {
	private float[] vertices;
	private short[] indices;

	private FloatBuffer vertexBuffer;
	private ShortBuffer indexBuffer;

	public Circle() {
		
		mNumVerts = 8;
		
		vertices = new float[mNumVerts * 3];
		indices = new short[mNumVerts];
		
		for(int i = 0; i < mNumVerts; i++)
		{
			indices[i] = (short) i;
			
			vertices[i * 3] = 0.02f * (float) Math.cos(2.0d * Math.PI * (double) i / mNumVerts);
			vertices[i * 3 + 1] = 0.02f * (float) Math.sin(2.0d * Math.PI * (double) i / mNumVerts);
			vertices[i * 3 + 2] = 0.0f;
		}
		
		
		ByteBuffer vbb = ByteBuffer.allocateDirect(vertices.length * 4);
		vbb.order(ByteOrder.nativeOrder());
		vertexBuffer = vbb.asFloatBuffer();
		vertexBuffer.put(vertices);
		vertexBuffer.position(0);

		ByteBuffer ibb = ByteBuffer.allocateDirect(indices.length * 2);
		ibb.order(ByteOrder.nativeOrder());
		indexBuffer = ibb.asShortBuffer();
		indexBuffer.put(indices);
		indexBuffer.position(0);
	}

	/**
	 * This function draws our Circle on screen.
	 * @param gl
	 */
	public void draw(GL10 gl) {
		// Counter-clockwise winding.
		gl.glFrontFace(GL10.GL_CCW); 
		// Enable face culling.
		gl.glEnable(GL10.GL_CULL_FACE); 
		// What faces to remove with the face culling.
		gl.glCullFace(GL10.GL_BACK); 

		// Enabled the vertices buffer for writing and to be used during
		// rendering.
		gl.glEnableClientState(GL10.GL_VERTEX_ARRAY);
		// Specifies the location and data format of an array of vertex
		// coordinates to use when rendering.
		gl.glVertexPointer(3, GL10.GL_FLOAT, 0, 
                                 vertexBuffer);

		gl.glDrawElements(GL10.GL_TRIANGLE_FAN, indices.length, GL10.GL_UNSIGNED_SHORT, indexBuffer);

		// Disable the vertices buffer.
		gl.glDisableClientState(GL10.GL_VERTEX_ARRAY); 
		// Disable face culling.
		gl.glDisable(GL10.GL_CULL_FACE); 
	}

	private int mNumVerts;
}
